using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class ChatFMessage : BaseChatElement
	{
		private const string TAG = "ChatFMessage";
		private Button ContentButton;
		private MenuItem ContentMenuItem;
		private IFmessageInfo mFmsgInfo;
		public static DependencyProperty FlagImgSourceProperty = DependencyProperty.Register("FlagImgSource", typeof(ImageSource), typeof(ChatFMessage), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/ffmsg/qq.png") as ImageSource, new PropertyChangedCallback(ChatFMessage.OnFlagImgSourceChanged)));
		public static DependencyProperty HeadImgSourceProperty = DependencyProperty.Register("HeadImgSource", typeof(ImageSource), typeof(ChatFMessage), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/CommonUse/DefaultHead.png") as ImageSource, new PropertyChangedCallback(ChatFMessage.OnHeadImgSourceChanged)));
		public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ChatFMessage), new PropertyMetadata(strings.Con_FMsg_TxtName, new PropertyChangedCallback(ChatFMessage.OnTitleChanged)));
		public static DependencyProperty NickNameProperty = DependencyProperty.Register("NickName", typeof(string), typeof(ChatFMessage), new PropertyMetadata("用户昵称", new PropertyChangedCallback(ChatFMessage.OnNickNameChanged)));
		public static DependencyProperty DetailStrProperty = DependencyProperty.Register("DetailStr", typeof(string), typeof(ChatFMessage), new PropertyMetadata("", new PropertyChangedCallback(ChatFMessage.OnDetailStrChanged)));
		public static DependencyProperty UserInfoProperty = DependencyProperty.Register("UserInfo", typeof(string), typeof(ChatFMessage), new PropertyMetadata("微信号:XXX", new PropertyChangedCallback(ChatFMessage.OnUserInfoChanged)));
		private bool isHeadInit;
		private EventWatcher mWatcherHeadImgAdd;
		private bool _contentLoaded;
		public ImageSource FlagImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatFMessage.FlagImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.FlagImgSourceProperty, value);
			}
		}
		public ImageSource HeadImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatFMessage.HeadImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.HeadImgSourceProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(ChatFMessage.TitleProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.TitleProperty, value);
			}
		}
		public string NickName
		{
			get
			{
				return (string)base.GetValue(ChatFMessage.NickNameProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.NickNameProperty, value);
			}
		}
		public string DetailStr
		{
			get
			{
				return (string)base.GetValue(ChatFMessage.DetailStrProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.DetailStrProperty, value);
			}
		}
		public string UserInfo
		{
			get
			{
				return (string)base.GetValue(ChatFMessage.UserInfoProperty);
			}
			set
			{
				base.SetValue(ChatFMessage.UserInfoProperty, value);
			}
		}
		public ChatFMessage()
		{
			this.InitializeComponent();
			this.DetailStr = strings.FmsgDetailBtn;
			base.add_Loaded(new RoutedEventHandler(this.ChatFMessage_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ChatFMessage_Unloaded));
		}
		private void ChatFMessage_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderUnit();
		}
		private void ChatFMessage_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderInit();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ContentButton = (base.GetTemplateChild("ContentButton") as Button);
			this.ContentMenuItem = (base.GetTemplateChild("DeleteMsgMenu") as MenuItem);
			this.ContentButton.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			this.ContentButton.add_Click(new RoutedEventHandler(this.ContentButton_Click));
			this.ContentMenuItem.Click += new RoutedEventHandler(this.ContentMenuItem_Click);
		}
		private void ContentButton_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		private void ContentMenuItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.deleteTalkMsg(this.chatMsg_0);
		}
		private void RefreshItem(ChatMsg chatMsg_1)
		{
			if (chatMsg_1 == null)
			{
				return;
			}
			this.mFmsgInfo = FMessageHelper.GetFmessageInfo(chatMsg_1);
			if (this.mFmsgInfo == null)
			{
				base.set_Height(0.0);
				base.set_Visibility(1);
				return;
			}
			this.Title = this.mFmsgInfo.Title;
			this.NickName = this.mFmsgInfo.NickName;
			this.UserInfo = this.mFmsgInfo.UserInfo;
			this.HeadImgSource = HeadImgMgr.getHeadImage(this.mFmsgInfo.UserName, true);
			this.FlagImgSource = this.mFmsgInfo.FlagImg;
			Contact contact = StorageMgr.contact.method_0(this.mFmsgInfo.UserName);
			if (contact != null)
			{
				ContactMgr.isContact(contact);
			}
		}
		public void ItemClickHandle(ChatMsg chatMsg_1)
		{
			if (chatMsg_1 == null)
			{
				return;
			}
			if (this.mFmsgInfo == null)
			{
				return;
			}
			MainPage mainPageInstance = MainPage.mainPageInstance;
			if (mainPageInstance == null)
			{
				return;
			}
			ContactDetailViewArgs contactDetailViewArgs = null;
			if (this.mFmsgInfo is FMessageValidInfo)
			{
				FMessageValidInfo fMessageValidInfo = this.mFmsgInfo as FMessageValidInfo;
				if (fMessageValidInfo == null)
				{
					return;
				}
				contactDetailViewArgs = ContactDetailViewForAll.getConDetailArgFromFMessageValidInfo(fMessageValidInfo);
			}
			else
			{
				if (this.mFmsgInfo is FMessageFriendInfo)
				{
					FMessageFriendInfo fMessageFriendInfo = this.mFmsgInfo as FMessageFriendInfo;
					if (fMessageFriendInfo == null)
					{
						return;
					}
					contactDetailViewArgs = ContactDetailViewForAll.getConDetailArgFromFMessageFriendInfo(fMessageFriendInfo);
				}
			}
			if (contactDetailViewArgs == null)
			{
				return;
			}
			mainPageInstance.GoToPage("/Source/UI/Contact/ContactDetailViewForAll.xaml", new object[]
			{
				contactDetailViewArgs
			}, false);
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			this.RefreshItem(Msg);
		}
		private static void OnFlagImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnHeadImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnTitleChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnNickNameChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnDetailStrChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnUserInfoChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void GetHeaderInit()
		{
			if (this.isHeadInit)
			{
				return;
			}
			this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("ChatFMessage", "registerEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = true;
		}
		private void GetHeaderUnit()
		{
			if (!this.isHeadInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("ChatFMessage", "removeEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = false;
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject == null)
			{
				return;
			}
			if (this.mFmsgInfo == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mFmsgInfo.UserName))
			{
				return;
			}
			using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && current.GetType() == typeof(string) && current == this.mFmsgInfo.UserName)
					{
						this.HeadImgSource = HeadImgMgr.getHeadImage(this.mFmsgInfo.UserName, true);
						break;
					}
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/ChatListItem/ChatFMessage.xaml", 2));
		}
	}
}
